library(tidyverse)
library(plotly)
head(combined_df)
##       CONTA GRUPO_TARIF CLASSE MES_REFERENCIA                  LOGRADOURO
## 1 412395272          B3      3         200901            AV SANTOS DUMONT
## 2 400106356          A4      3         200901 R GUSTAVO CORDEIRO DE FARIA
## 3 400106380          A4      3         200901            AV HELDER CAMARA
## 4 400106410          A4      2         200901              R ABDALA CHAMA
## 5 400106445          A4      3         200901            AV HELDER CAMARA
## 6 400106461          A4      2         200901            AV HELDER CAMARA
##   NUMERO COMPLEMENTO   BAIRRO SUB_BAIRRO      CEP CONSUMO_MENSAL_KWH
## 1      1             BENFICA     BENFICA 20930040               1790
## 2     84             BENFICA     BENFICA 20910220             178159
## 3    301             BENFICA     BENFICA 20973011               6350
## 4    183             BENFICA     BENFICA 20911250               5458
## 5     79             BENFICA     BENFICA 20973011             159970
## 6    186      NN 188 BENFICA     BENFICA 20973011              10854
combined_df_summary <- combined_df %>%
  group_by(BAIRRO, MES_REFERENCIA) %>%
  summarise(total_consumo_kwh = sum(CONSUMO_MENSAL_KWH, na.rm = TRUE))
## `summarise()` has grouped output by 'BAIRRO'. You can override using the
## `.groups` argument.
# Convert MES_REFERENCIA to a proper Date format
combined_df_summary <- combined_df_summary %>%
  mutate(MES_REFERENCIA = as.Date(paste0(MES_REFERENCIA, "01"), format = "%Y%m%d"))



novo_bairro <- combined_df_summary %>%
  group_by(MES_REFERENCIA) %>%
  summarise(total_consumo_kwh = sum(total_consumo_kwh)) %>%
  mutate(BAIRRO = "RIO DE JANEIRO")




combined_df_summary <- rbind(novo_bairro, combined_df_summary)

write.csv(combined_df_summary, "C:/Users/rodri/OneDrive - Insper/ConsumoEnergiaRJ/DadosCompletos.csv")
# Create an interactive time series plot
interactive_plot <- plot_ly(data = combined_df_summary, 
                            x = ~MES_REFERENCIA, 
                            y = ~total_consumo_kwh, 
                            type = 'scatter', 
                            mode = 'lines+markers', 
                            color = ~BAIRRO,  
                            text = ~paste('Bairro:', BAIRRO, '<br>Total Consumption (kWh):', total_consumo_kwh, '<br>Month:', MES_REFERENCIA),
                            hoverinfo = 'text',  # This line ensures that only the text info is shown on hover
                            name = ~BAIRRO,
                            line = list(shape = 'linear')) %>%
  layout(title = 'Monthly Energy Consumption Over Time',
         xaxis = list(title = 'Month'),
         yaxis = list(title = 'Total Consumption (kWh)'),
         hovermode = 'closest')


# Show the plot
interactive_plot
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors